/*
 * @Author: wangwenshuai 644586996@qq.com
 * @Date: 2023-04-25 15:46:43
 * @LastEditors: wangwenshuai 644586996@qq.com
 * @LastEditTime: 2023-05-15 16:54:38
 * @FilePath: \carbon_jee\src\permission.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Vue from 'vue'
import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { ACCESS_TOKEN, INDEX_MAIN_PAGE_PATH, OAUTH2_LOGIN_PAGE_PATH } from '@/store/mutation-types'
// import { generateIndexRouter, isOAuth2AppEnv } from '@/utils/util'

const whiteList = ['/login','/register'] // no redirect whitelist


NProgress.configure({ showSpinner: false }) // NProgress Configuration
router.beforeEach((to, from, next) => {
    NProgress.start() // start progress bar
    if (Vue.ls.get(ACCESS_TOKEN)) {

        if (to.path === '/login') {
            next({ path: INDEX_MAIN_PAGE_PATH })
            NProgress.done()
        } else {

            if (store.getters.permissionList.length === 0) {
               
                store.dispatch('GetPermissionList').then(res => {
                    const menuData = res.result.menu;
                    if (menuData === null || menuData === "" || menuData === undefined) {
                        return;
                    }
                    //   console.log(menuData)
                })
            }
            const redirect = decodeURIComponent(from.query.redirect || to.path)
            if (to.path === redirect) {
                next()
            } else {
                // 跳转到目的路由
                next({ path: redirect })
            }
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            let path = '/login'
            next({ path: path, query: { redirect: to.fullPath } })
            NProgress.done()
        }


    }

})
router.afterEach(() => {
    NProgress.done() // finish progress bar
})